package com.hzf.wxplatform.web.controller.manager.cart;

import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.Arith;
import com.hzf.common.util.ByteUtils;
import com.hzf.common.util.EhCacheUtil;
import com.hzf.material.dao.model.*;
import com.hzf.order.dao.model.*;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.wxplatform.common.WxplatformConstant;
import com.hzf.wxplatform.common.WxplatformResult;
import com.hzf.wxplatform.common.WxplatformResultConstant;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.Id;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe: TODO
 * creat_user: 白开水
 * creat_time: 2017/12/4   16:47
 **/

@Controller
@RequestMapping("/orderCart")
public class OrderCartController extends BaseController {
    @Autowired
    ApiService apiService;

    @RequestMapping(value = "/index")
    private String index(Model model, HttpServletRequest request) {
        byte[] a = UcenterUserUtils.getUser(request.getSession());
        if(null==a){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser user = (UcenterUser) ByteUtils.ByteToObject(a);
        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
/*        OrderCartExample orderCartShopExample = new OrderCartExample();
        orderCartShopExample.createCriteria().andUserIdEqualTo(user.getId());
        List<OrderCart> list = apiService.selectOrderCartShop(orderCartShopExample);

        }*/


        OrderCartExample orderCartExample = new OrderCartExample();
        orderCartExample.createCriteria().andUserIdEqualTo(user.getId());
        List<OrderCart> list = apiService.selectByShopId(orderCartExample);
        for (OrderCart orderCart : list) {
            OrderCartExample orderCartExample1 = new OrderCartExample();
            orderCartExample1.createCriteria().andAShopidEqualTo(orderCart.getShopId()).andUserIdEqualTo(user.getId());
            List<OrderCart> shopGoods = apiService.selectByGoodsPrimaryKey(orderCartExample1);
            for(OrderCart orderCart1:shopGoods){
                System.out.println(orderCart1.getInstallId());
            }
            orderCart.setGoods(shopGoods);
            MaterialShopExample materialShopExample = new MaterialShopExample();
            materialShopExample.createCriteria().andShopIdEqualTo(orderCart.getShopId());
            MaterialShop materialShop = apiService.selectByShop(materialShopExample);
            orderCart.setShops(materialShop);
        }
        model.addAttribute("result", list);
        return thymeleaf("/YZ/YZ_GWC_Shopping");
    }

    @RequestMapping(value = "/saveCart", method = RequestMethod.POST)
    @ResponseBody
    private Object saveCart(Model model, OrderCart orderCart, HttpServletRequest httpServletRequest) {
        byte[] a = UcenterUserUtils.getUser(httpServletRequest.getSession());
        UcenterUser user = (UcenterUser) ByteUtils.ByteToObject(a); if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        orderCart.setUserId(user.getId());
        int count = apiService.insertOrderCart(orderCart);
        if (count > 0) {
            if (count == 3) {
                return new WxplatformResult(WxplatformResultConstant.FAILED, "商品已存在您的购物车");

            }
            return new WxplatformResult(WxplatformResultConstant.SUCCESS, "添加购物车成功");
        } else {
            return new WxplatformResult(WxplatformResultConstant.FAILED, "添加购物车失败，请稍后再试");

        }

    }

    @RequestMapping(value = "/checkCart", method = RequestMethod.POST)
    @ResponseBody
    private Object checkCart(Model model,String orderCart, HttpServletRequest httpServletRequest) {
        byte[] a = UcenterUserUtils.getUser(httpServletRequest.getSession());
        UcenterUser user = (UcenterUser) ByteUtils.ByteToObject(a); if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        JSONArray json =  JSONArray.fromObject(orderCart);
        System.out.println(json.get(1));
        System.out.println(json.toString());
        List<OrderCart> orderCarts = (List)JSONArray.toCollection(json,OrderCart.class);

        for(OrderCart orderCart1 :orderCarts){
            OrderCartExample example = new OrderCartExample();
            example.createCriteria().andCartIdEqualTo(orderCart1.getCartId()).andUserIdEqualTo(user.getId());
            List<OrderCart> list = apiService.selectByGoodsPrimaryKey(example);
            if (list.size() > 0) {
                OrderCartExample orderCartExample = new OrderCartExample();
                orderCartExample.createCriteria().andCartIdEqualTo(orderCart1.getCartId()).andCountEqualTo(orderCart1.getCount());
                int i = apiService.updateCart(orderCart1);

            } else {
                return new WxplatformResult(WxplatformResultConstant.FAILED, "此商品不在您购物车内，请重新操作");
            }

        }
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, "正在跳转请稍后。。。。。。");




    }

    @RequestMapping(value = "/toOrderCart", method = RequestMethod.GET)
    public String toOrderCart(HttpServletRequest request, Model model,String type, String id) {
        byte[] a = UcenterUserUtils.getUser(request.getSession());
        UcenterUser user = (UcenterUser) ByteUtils.ByteToObject(a); if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        if(type.equals("0")) {
            String[] ids = id.split("\\,");
            List<OrderCart> list = new ArrayList<>();
            List<OrderCart> shoplist = new ArrayList<>();
            int size = 0;
            double zongjia = 0.00;
            for (String i : ids) {
                OrderCartExample orderCartExample = new OrderCartExample();
                orderCartExample.createCriteria().andUserIdEqualTo(user.getId()).andCartIdEqualTo(Integer.parseInt(i));
                List<OrderCart> list1 = apiService.selectByShopId(orderCartExample);
                if (null != list) {
                    if (0 != list.size()) {
                        if (null != list1) {
                            if (0 != list1.size()) {
                                for (OrderCart orderCart : list) {
                                    OrderCart orderCart1 = list1.get(0);
                                    if (orderCart1.getShopId() != orderCart.getShopId()) {
                                        shoplist.add(orderCart1);
                                    }
                                }
                            } else {
                                return thymeleaf("/YZ/YZ_GWC_Shopping");
                            }
                        } else {
                            return thymeleaf("/YZ/YZ_GWC_Shopping");
                        }
                    } else {
                        shoplist.add(list1.get(0));
                        list.add(list1.get(0));
                    }
                } else {
                    shoplist.add(list1.get(0));
                    list.add(list1.get(0));
                }
                for (OrderCart orderCart : shoplist) {
                    List<OrderCart> list2 = new ArrayList<>();
                    if (null != orderCart.getGoods()) {
                        if (0 != orderCart.getGoods().size()) {
                            list2 = orderCart.getGoods();
                        }
                    }
                    OrderCartExample example = new OrderCartExample();
                    example.createCriteria().andCartIdEqualTo(Integer.parseInt(i)).andUserIdEqualTo(user.getId());
                    List<OrderCart> orderlist = apiService.selectByGoodsPrimaryKey(example);
                    if (null != orderlist) {
                        if (0 != orderlist.size()) {
                            OrderCart orderCart1 = orderlist.get(0);
                            if (orderCart1.getShopId() == orderCart.getShopId()) {
                                list2.add(orderCart1);
                                orderCart.setGoods(list2);
                                MaterialShopExample materialShopExample = new MaterialShopExample();
                                materialShopExample.createCriteria().andShopIdEqualTo(orderCart.getShopId());
                                MaterialShop materialShop = apiService.selectByShop(materialShopExample);
                                orderCart.setShops(materialShop);
                            }
                        }
                    }
                }
            }
            for (OrderCart orderCart : shoplist) {
                size = size + orderCart.getGoods().size();
                for (OrderCart orderCart1 : orderCart.getGoods()) {
                    if (orderCart1.getInstallId() == 1) {
                        double zong = orderCart1.getCount() * orderCart1.getModel().getPrice();

                        zong = zong + orderCart1.getCount() * orderCart1.getMaterialGoods().getInstallPrice();

                        zongjia = Arith.add(zongjia, zong);

                    } else {
                        double zong = orderCart1.getCount() * orderCart1.getModel().getPrice();

                        zongjia = Arith.add(zongjia, zong);

                    }

                }
            }
            if (null != EhCacheUtil.get("hzf-wxplatform-web-ehcache", user.getId() + "dingdan")) {
                EhCacheUtil.remove("hzf-wxplatform-web-ehcache", user.getId() + "dingdan");
            }
            if (null != EhCacheUtil.get("hzf-wxplatform-web-ehcache", user.getId() + "dingdanList")) {
                EhCacheUtil.remove("hzf-wxplatform-web-ehcache", user.getId() + "dingdanList");
            }
            EhCacheUtil.put("hzf-wxplatform-web-ehcache", user.getId() + "dingdan", id);

            EhCacheUtil.put("hzf-wxplatform-web-ehcache", user.getId() + "dingdanList", shoplist);
            OrderUserAddress address = new OrderUserAddress();
            if (null != EhCacheUtil.get("hzf-wxplatform-web-ehcache", user.getId() + "dizhi")) {
                address = (OrderUserAddress) EhCacheUtil.get("hzf-wxplatform-web-ehcache", user.getId() + "dizhi");
            }
            model.addAttribute("address", address);
            model.addAttribute("result2", shoplist);
            model.addAttribute("zongjia", zongjia);
            model.addAttribute("shuliang", size);

            return thymeleaf("/YZ/YZ_GWC_TijiaoTick");
        }else if(type.equals("1")){
            List<OrderCart> list= new ArrayList<>();

            OrderUserAddress address = new OrderUserAddress();
            if (null != EhCacheUtil.get("hzf-wxplatform-web-ehcache", user.getId() + "dizhi")) {
                address = (OrderUserAddress) EhCacheUtil.get("hzf-wxplatform-web-ehcache", user.getId() + "dizhi");
            }
            OrderCart orderCart =(OrderCart) EhCacheUtil.get("hzf-wxplatform-web-ehcache",user.getId()+"zhijiedingdan");

            double jiage = orderCart.getModel().getPrice()*orderCart.getCount();
            model.addAttribute("orderCart", orderCart);
            model.addAttribute("zongjia", jiage);
            model.addAttribute("shuliang", orderCart.getCount());
            model.addAttribute("address", address);
            return thymeleaf("/YZ/YZ_ORDER_TijiaoTick");

        }else if(type.equals("3")){
            OrderUserAddress address = new OrderUserAddress();
            if (null != EhCacheUtil.get("hzf-wxplatform-web-ehcache", user.getId() + "dizhi")) {
                address = (OrderUserAddress) EhCacheUtil.get("hzf-wxplatform-web-ehcache", user.getId() + "dizhi");
            }
            OrderUser orderUser = (OrderUser)EhCacheUtil.get("hzf-wxplatform-web-ehcache",user.getId()+"xiugaidingdan");
            List<OrderUserInfo> list =apiService.selectByUserByOrderUser(orderUser.getId());
            orderUser.setOrderUserInfoList(list);
            model.addAttribute("address",address);
            model.addAttribute("zongjia",orderUser.getPayPrice());
            model.addAttribute("data",orderUser);
            model.addAttribute("shuliang", list.size());
            return thymeleaf("/YZ/YZ_ORDER_OrderTick");
        }
        return  null;
    }
}
